<?php

// 富文本编辑器的使用 与 普通 textarea 的区别
// pdo添加数据实现文章的添加和信息列表

// print_r($_SERVER);

// 如果是post请求，就获取提交的数据
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    // 获取提交的标题和内容
    $title = $_POST['title'];
    $content = $_POST['content'];
    
    // 执行插入语句 把文章标题和内容添加到表中
    // INSERT INTO `articles` (`title`, `content`) VALUES ( 'aaaa', 'bbb');
    
    include_once("../conn.php");
    
    $sql = "INSERT INTO `articles` (`title`, `content`) VALUES ( :title, :content)";

    
    // 预处理sql
    $stmt = $conn->prepare($sql);
    
    // 绑定参数
    $stmt->bindParam(":title", $title);
    $stmt->bindParam(":content", $content);
    
    // 执行sql语句
    $stmt->execute();
    
    
    // 如何判断我插入是否成功
    if ($stmt->rowCount()>0) {
        header("location: list.php");
        exit();
    } else {
        die("添加失败");
    }
}

?>

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
    <meta name="generator" content="Hugo 0.88.1">
    <title>后台管理</title>

    <link rel="canonical" href="https://getbootstrap.com/docs/5.1/examples/dashboard/">

    
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet">

<meta name="theme-color" content="#7952b3">


    <style>
      .bd-placeholder-img {
        font-size: 1.125rem;
        text-anchor: middle;
        -webkit-user-select: none;
        -moz-user-select: none;
        user-select: none;
      }

      @media (min-width: 768px) {
        .bd-placeholder-img-lg {
          font-size: 3.5rem;
        }
      }
    </style>

    
    <!-- Custom styles for this template -->
    <link href="../admin/css/dashboard.css" rel="stylesheet">
  </head>
  <body>
    
<header class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
  <a class="navbar-brand col-md-3 col-lg-2 me-0 px-3" href="#">东阳吴彦祖的博客</a>
  <button class="navbar-toggler position-absolute d-md-none collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#sidebarMenu" aria-controls="sidebarMenu" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <input class="form-control form-control-dark w-100" type="text" placeholder="Search" aria-label="Search">
  <div class="navbar-nav">
    <div class="nav-item text-nowrap">
      <a class="nav-link px-3" href="#">Sign out</a>
    </div>
  </div>
</header>

<div class="container-fluid">
  <div class="row">
    <nav id="sidebarMenu" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">
      <div class="position-sticky pt-3">
      <ul class="nav flex-column">
          <li class="nav-item">
            <a class="nav-link active" aria-current="page" href="/admin/">
              <span data-feather="home"></span>
              后台首页
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="./list.php">
              <span data-feather="file"></span>
              文章列表
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="./add.php">
              <span data-feather="file"></span>
              添加文章
            </a>
          </li>
        </ul>
      </div>
    </nav>

    <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
	<form action="" method="post">
		
		<div>博客标题：</div>
		<div>
			<input class="form-control" type="text" name="title" placeholder="请输入标题">
		</div>
		<textarea name="content" hidden="hidden" id="content" cols="30" rows="10"></textarea>
		<div>博客内容：</div>
		<div id="div1"></div>
		<div>
			<input class="btn btn-primary" type="submit" value="提交">
		</div>
	</form>
    </main>
  </div>
</div>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.0.2/js/bootstrap.bundle.min.js"></script>
    <script src="../admin/js/dashboard.js"></script>

	<script src="https://unpkg.com/wangeditor/dist/wangEditor.min.js"></script>
	<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
	
	<script>
		
		// 获取textarea元素
		const $content = $('#content')
		
		// 创建编辑器对象
		const editor = new window.wangEditor("#div1");
		
		// 监控变化，同步更新到 textarea，  每次富文本编辑器内容改变都会调用这个function
		editor.config.onchange = function (html) {
			// 修改textarea的值
			$content.val(html)
		}
		editor.create();
		
		// 第一步，初始化 textarea 的值
		$content.val(editor.txt.html())
		
	</script>
  </body>
</html>
